﻿using System;
using System.Text;

public class BinaryToHexadecimalDirectlyConversion
{
    public static void Main()
    {
        BinaryToHexadecimalDirectly("Problem 6: Binary to Hexadecimal Directly conversion");
    }

    public static void BinaryToHexadecimalDirectly(string description)
    {
        string number = ConversionBetweenNumeralSystems.InputNumber(description, 2);
        string minus = "";
        int rest;
        StringBuilder construct = new StringBuilder();
        StringBuilder four = new StringBuilder(4);

        if (number[0] == '-')
        {
            minus = "-";
            number = number.Remove(0, 1);
        }

        number = ConversionBetweenNumeralSystems.RemoveLeadingZeros(number);
        rest = number.Length % 4;

        for (int i = 0; i <= number.Length; i++)
        {
            if ((rest <= i) && ((i - rest) % 4 == 0) && (0 < i))
            {
                construct.Append(ConversionBetweenNumeralSystems.Convert(ConversionBetweenNumeralSystems.RemoveLeadingZeros(four.ToString()), 2, 16));
                four.Clear();
            }
            
            if (i < number.Length)
                four.Append(number[i]);
        }

        Console.Clear();
        Console.WriteLine(description + "\n" + minus + number + "(2) = " + minus + construct.ToString() + "(16)");
    }
}